<!DOCTYPE html>
<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8"
    contentType="text/html;charset=utf-8"%> 
<html>
<head>
<title>资源管理-新建资源</title>
<jsp:include page="/common/head.jsp"></jsp:include>
<link href="<%=request.getContextPath()%>/static/plugin/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet">
<script src="<%=request.getContextPath()%>/static/plugin/zTree/js/jquery.ztree.all-3.5.min.js"></script>  
</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>资源管理</span>
            <span class="split"></span>
            <span>新建资源</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    <div class="page-title-border"></div>
</div>
<form id="resourceForm">
<div class="form-wrapper">
        <div class="form-cont">
            <p class="form-cont-title"><span class="field-required">*</span>上级节点</p>
             <input type="hidden" name="pId" value="${pId}" id="pId"/>
             <input type="text" id="parentName" class="summer-input distance-right" value="${parentName}" readonly/>
            <label class="label-secondary "></label>
        </div>
        
        <div class="form-cont">
            <p class="form-cont-title"><span class="field-required">*</span>资源名称</p>
             <input type="text" id="name" name="name" class="summer-input summer-validatebox distance-right" data-validatebox="validType:['required','maxLength[100]']"/>
            <label class="label-secondary "></label>
        </div>
        
         <div class="form-cont">
            <p class="form-cont-title"><span class="field-required">*</span>资源简称</p>
             <input type="text" id="sName" name="sName" class="summer-input summer-validatebox distance-right" data-validatebox="validType:['required','maxLength[50]']"/>
            <label class="label-secondary "></label>
        </div>
        
        <div class="form-cont">
            <p class="form-cont-title"><span class="field-required">*</span>资源类别</p>
             <input type="text" id="type" name="type" class="summer-input summer-validatebox distance-right" data-validatebox="validType:['required']"/>
            <label class="label-secondary "></label>
        </div>
        
        <div class="form-cont">
            <p class="form-cont-title">URL</p>
             <input type="text" id="url" name="url" class="summer-input summer-validatebox distance-right" data-validatebox="validType:['maxLength[200]']"/>
            <label class="label-secondary ">资源类别为菜单及功能时需填写URL</label>
        </div>
        
         <div class="form-cont">
            <p class="form-cont-title"><span class="field-required">*</span>权限标识</p>
             <input type="text" id="permit" name="permit" class="summer-input distance-right" />
            <label class="label-secondary ">用于标识该权限的唯一字符串,格式为菜单:功能,如user:add</label>
        </div>
        
        <div class="form-cont">
            <p class="form-cont-title">功能标识</p>
            <input type="text" id="mark" name="mark" class="summer-input distance-right" />
            <label class="label-secondary ">用于标识该功能的类型</label>
        </div>
        
        <div class="form-cont">
            <p class="form-cont-title">ModelView</p>
            <input type="text" id="returnPath" name="returnPath" class="summer-input distance-right summer-validatebox" data-validatebox="validType:['maxLength[50]']"/>
            <label class="label-secondary ">返回页面的映射地址</label>
        </div>
        
        <div class="form-cont">
            <p class="form-cont-title"><span class="field-required">*</span>排序号</p>
            <input type="text" id="orderNo" name="orderNo" class="summer-input summer-validatebox distance-right" data-validatebox="validType:['required','betweenInteger[1,100000]']"/>
            <label class="label-secondary "></label>
        </div>        
</div>

<div class="form-btn-wrapper">
    <a class="btn btn-normal btn-ok distance-right" id="saveBtn">保存</a>
    <!-- <a class="btn btn-normal btn-ok" id="saveBtn1">保存并继续</a> -->
</div>
</form>
        
     <script>
         var RESOURCE_URL = {
             INIT_STAT: basePath + '/sys/resource/initStat',
             ADD: basePath + '/sys/resource/add',
             CHECKEXIST : basePath + '/sys/resource/isExist'
         },
         level = ${level};
     
        $(function() {
            $("#type").combobox({
                valueField: 'code',
                textField:  'name',
                menuHeight: 130,
                url : COMMON_URL.getCode + "?type=T_DM_RESOURCE_TYPE",
                onLoadSuccess: function(){
                	$('#type').combobox("setValue",level + 1); 
                }
            });
            $('#mark').combobox({
                valueField: 'code',
                textField: 'name',
                menuHeight:200,
                url : COMMON_URL.getCode + "?type=T_DM_RESOURCE_MARK"
            }); 
            
            $('#saveBtn1').click(function(){
            	save(true);
            });
            $('#saveBtn').click(function(){
                save(false);
            });
            // 返回按钮事件
            $('#goBack').on('click',goBack);
            //校验代码code是否可用   
            validateExist('#permit','permit', RESOURCE_URL.CHECKEXIST, ['required','maxLength[50]','regexp'], "标识已存在");
        });
        
        function save(isContinue) {
             $("#resourceForm").form({
            	 buttons : ['#saveBtn','#saveBtn1'],
                 ajax: {
                     url: RESOURCE_URL.ADD
                 },
                 onSubmitSuccess: function(data){
                	 $.stip({
   	                     type:'ok',//显示类型 有info、 ok、 danger
   	                     text: '保存成功！',
   	                     onBeforeHide: function () {
   	                    	if (isContinue){
                                parent.refreshPage('${pId }', true, level); 
                                formReset();
                            } else {
                                parent.refreshPage('${pId }');
                            }
   	                    }
   	                 });
                	 
                 }
             }).form("submit");
        }
        
        // 返回
        function goBack() {
            window.location.href=RESOURCE_URL.INIT_STAT;
        }
        
        function formReset(){
        	$("#name,#sName,#permit,#url,#orderNo,#returnPath").val("");
        	$('#type').combobox("setValue",level + 1);
        	$('#mark').combobox("clear");
        }
        // 校验代码权限标识是否重复
        function validateExist(id, name, url, validType, message) {
            $(id).validatebox({
                rules: {
                    regexp: {
                        validator: function (value,param) {
                            var result = false,
                                params = {}, 
                                key = name;
                                params[key] = value;
                                params['id'] = $("#id").val();
                            $.ajax({
                                type: 'post',
                                url: url,
                                data: params,
                                dataType: 'json',
                                async: false,
                                success: function (data) {
                                    result = !(data.result == 'exist');
                                }
                            });
                            return result;
                        },
                        message: message,
                    }
                },
                validType: validType
            });
        }
     </script>
</body>
</html>